package com.zhongbo.java.fast.order.web.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.zhongbo.java.fast.order.exception.BusinessException;
import com.zhongbo.java.fast.order.model.Item;
import com.zhongbo.java.fast.order.model.Order;
import com.zhongbo.java.fast.order.service.IOrderService;
import com.zhongbo.java.fast.order.util.ItemUtil;
import com.zhongbo.java.fast.order.util.LocalDateTimeUtil;
import com.zhongbo.java.fast.order.vo.RestResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年03月27日 星期四 11:42:03
 */
@RestController
@RequestMapping("order")
@Api(tags = "订单接口")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping("createOrder")
    @ApiOperation("创建订单（带时间模拟参数）")
    RestResultVo<Order> createOrder(@RequestBody List<Item> items, boolean isVIP, String currentTime) throws BusinessException {
        DateTime parse = DateUtil.parse(currentTime);
        LocalDateTime convert = LocalDateTimeUtil.convert(parse);
        Order order = orderService.createOrder(items, isVIP, convert);
        return RestResultVo.ok(order);
    }

    @GetMapping("cancelOrder")
    @ApiOperation("取消订单（带时间模拟参数）")
    RestResultVo<String> cancelOrder(String orderId, LocalDateTime currentTime) throws BusinessException {
        orderService.cancelOrder(orderId, currentTime);
        return RestResultVo.success();
    }

    @GetMapping("findOrder")
    @ApiOperation("查询订单")
    RestResultVo<Order> findOrder(String orderId, String currentTime) {
        DateTime parse = DateUtil.parse(currentTime);
        LocalDateTime convert = LocalDateTimeUtil.convert(parse);
        Order order = orderService.findOrder(orderId, convert);
        return RestResultVo.ok(order);
    }

    @GetMapping("listOrder")
    @ApiOperation("查询全部订单")
    RestResultVo<List<Order>> listOrder() {
        List<Order> orderList = orderService.listOrder();
        return RestResultVo.ok(orderList);
    }

    @GetMapping("listItem")
    @ApiOperation("查询全部商品列表")
    RestResultVo<List<Item>> listItem() {
        return RestResultVo.ok(ItemUtil.getAllItems());
    }

}
